package com.wck.car.oyz;

import com.wck.car.model.CarBrandDesc;
import com.wck.car.model.CarBrandInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.List;

public class CarBrandDescType {


    /**
     * 获得品牌车辆详情
     *
     * @param brandInfo
     * @return
     */

    public static List<CarBrandDesc> getCarHtml(CarBrandInfo brandInfo) {
        String carBody = SpiderCar.getBody("https://m.evpartner.com/Operator/Home/Brand?id=" + brandInfo.getId());
        Document document = Jsoup.parse(carBody);
        brandInfo.setDesc(document.getElementsByClass("jianjie").text());  //获得品牌简介
        Elements ul = document.getElementsByClass("car_lists").get(0).getElementsByTag("ul");
        Elements li = ul.get(0).getElementsByTag("li");
        List<CarBrandDesc> carBrandDescsList = new ArrayList<>();
        li.forEach(e -> {
            CarBrandDesc carBrandDesc = new CarBrandDesc();
            Elements dt = e.getElementsByTag("dt");
            Element img = dt.get(0).getElementsByTag("img").get(0);
            carBrandDesc.setCarImg(img.attr("src"));   //获得车辆图片
            Elements dd = e.getElementsByTag("dd");
            Element aTag = dd.select("a[href]").get(0);
            carBrandDesc.setCarName(aTag.text());                //获得车辆名称
            carBrandDesc.setCarLink(aTag.attr("href"));       //获得车辆详情链接
            String id = aTag.attr("href").split("-")[1].split("\\.")[0];
            carBrandDesc.setId(Long.parseLong(id));     //获得车辆ID
            Element p = dd.select("p").get(0);
            carBrandDesc.setCarEndurance(p.text());      //获得车辆续航
            Element p1 = dd.select("p").get(1);
            carBrandDesc.setCarPrice(p1.getElementsByTag("span").text());   //获得车辆指导价
            carBrandDesc.setCarId(brandInfo.getId());
            carBrandDescsList.add(carBrandDesc);
        });
        return carBrandDescsList;
    }

}
